/*
	FileName : JT_C_COMM_SM.h
	Description :
	Created on 2017.11.18 by WFL.

	Revision History:

*/



#ifndef  __JT_C_COMM_SM_20171118__
#define  __JT_C_COMM_SM_20171118__


#include "stdint.h"



////------------------------------------------------------------------------

#define  JTC_COMM_SM_RX_DATA_MAX_LEN	1024
#define  JTC_COMM_SM_TX_DATA_MAX_LEN	1024

//---------------------------------------------------------


////////////////////////////////////////////////////
///external and internal version
////////////////////////////////////////////////////
/*
	External:
		sample		BIOS, APP
		A control	BIOS, APP
		  decode1	BIOS, APP
		  decode2	BIOS, APP
		B control	BIOS, APP
		  decode1	BIOS, APP
		  decode2	BIOS, APP
		lianjie		BIOS, APP
	Internal:
		sample		BIOS, APP
		A control	BIOS, APP
		  decode1	BIOS, APP
		  decode2	BIOS, APP
		B control	BIOS, APP
		  decode1	BIOS, APP
		  decode2	BIOS, APP
		lianjie		BIOS, APP
*/

#define  JTC_COMM_VERSION_CNT			(8 * 2)			//COMM: communication
#define  JTC_COMM_VERSION_DATALEN		(8 * 2 * 14)

#define  JTC_COMM_VerIndex_Ex_JLSAM		0	//Ex: external
#define  JTC_COMM_VerIndex_Ex_ZKA		1
#define  JTC_COMM_VerIndex_Ex_JMA1		2
#define  JTC_COMM_VerIndex_Ex_JMA2		3
#define  JTC_COMM_VerIndex_Ex_ZJB		4
#define  JTC_COMM_VerIndex_Ex_JMB1		5
#define  JTC_COMM_VerIndex_Ex_JMB2		6
#define  JTC_COMM_VerIndex_Ex_LJB		7

#define  JTC_COMM_VerIndex_In_JLSAM		8	//In: internal
#define  JTC_COMM_VerIndex_In_ZKA		9
#define  JTC_COMM_VerIndex_In_JMA1		10
#define  JTC_COMM_VerIndex_In_JMA2		11
#define  JTC_COMM_VerIndex_In_ZKB		12
#define  JTC_COMM_VerIndex_In_JMB1		13
#define  JTC_COMM_VerIndex_In_JMB2		14
#define  JTC_COMM_VerIndex_In_LJB		15
////------------------------------------------------------------------------
#define  JTC_COMM_SM_TICK_50ms			50
#define  JTC_COMM_SM_TICK_100ms			100

////------------------------------------------------------------------------
#define  JTC_COMM_CMDTYPE_FIRMWARE			3
#define  JTC_COMM_CMDTYPE_WORK_INFO			4
#define  JTC_COMM_CMDTYPE_DEBUG				0x0A
//-----------------------------------
#define  JTC_COMM_CMD_INQUIRY_WORK_INFO			0x16
#define  JTC_COMM_CMD_INQUIRY_110V_INFO			0x18
#define  JTC_COMM_CMD_TIME_BROAD_CAST			0x20
#define  JTC_COMM_CMD_XFER_STATUS_INFO			0x3A
#define  JTC_COMM_CMD_XFER_JW_INFO				0x3B
#define  JTC_COMM_CMD_XFER_BOARD_VERSION_INFO	0x3D

#define  JTC_COMM_CMD_INQUIRY_Ex_VERSION		0x09
#define  JTC_COMM_CMD_INQUIRY_In_VERSION		0x27
//-----------------------------------------------------------------------

#define  JTC_COMM_BDADDR_JLB_MAIN		0x20
#define  JTC_COMM_BDADDR_ZKA			0X30
#define  JTC_COMM_BDADDR_JMA1			0X31
#define  JTC_COMM_BDADDR_JMA2			0X32
#define  JTC_COMM_BDADDR_ZKB			0X40
#define  JTC_COMM_BDADDR_JMB1			0X41
#define  JTC_COMM_BDADDR_JMB2			0X42
#define  JTC_COMM_BDADDR_LJB			0X50

//-------------------------------------
#define  ACK_LEN_CTRL					72
#define  ACK_LEN_110V					18

////------------------------------------------------------------------------
#define  JTC_COMM_SM_STATE_IDEL			0

#define  JTC_COMM_SM_STATE_i_ZKA		10		//i: inquiry
#define  JTC_COMM_SM_STATE_w_ZKA		11		//w: wait
#define  JTC_COMM_SM_STATE_i_ZKB		12
#define  JTC_COMM_SM_STATE_w_ZKB		13
#define  JTC_COMM_SM_STATE_i_LJB		14
#define  JTC_COMM_SM_STATE_w_LJB		15

#define  JTC_COMM_SM_STATE_BROADCAST	20
#define  JTC_COMM_SM_STATE_s_REC		21		//s: send
#define  JTC_COMM_SM_STATE_w_REC		22

#define  JTC_COMM_SM_STATE_i_JW			30
#define  JTC_COMM_SM_STATE_w_JW			31

#define  JTC_COMM_SM_STATE_i_ExV_ZKA	40
#define  JTC_COMM_SM_STATE_w_ExV_ZKA	41
#define  JTC_COMM_SM_STATE_i_ExV_JMA1	42
#define  JTC_COMM_SM_STATE_w_ExV_JMA1	43
#define  JTC_COMM_SM_STATE_i_ExV_JMA2	44
#define  JTC_COMM_SM_STATE_w_ExV_JMA2	45
#define  JTC_COMM_SM_STATE_i_ExV_ZKB	46
#define  JTC_COMM_SM_STATE_w_ExV_ZKB	47
#define  JTC_COMM_SM_STATE_i_ExV_JMB1	48
#define  JTC_COMM_SM_STATE_w_ExV_JMB1	49
#define  JTC_COMM_SM_STATE_i_ExV_JMB2	50
#define  JTC_COMM_SM_STATE_w_ExV_JMB2	51
#define  JTC_COMM_SM_STATE_i_ExV_LJB	52
#define  JTC_COMM_SM_STATE_w_ExV_LJB	53

#define  JTC_COMM_SM_STATE_i_InV_ZKA	60
#define  JTC_COMM_SM_STATE_w_InV_ZKA	61
#define  JTC_COMM_SM_STATE_i_InV_JMA1	62
#define  JTC_COMM_SM_STATE_w_InV_JMA1	63
#define  JTC_COMM_SM_STATE_i_InV_JMA2	64
#define  JTC_COMM_SM_STATE_w_InV_JMA2	65
#define  JTC_COMM_SM_STATE_i_InV_ZKB	66
#define  JTC_COMM_SM_STATE_w_InV_ZKB	67
#define  JTC_COMM_SM_STATE_i_InV_JMB1	68
#define  JTC_COMM_SM_STATE_w_InV_JMB1	69
#define  JTC_COMM_SM_STATE_i_InV_JMB2	70
#define  JTC_COMM_SM_STATE_w_InV_JMB2	71
#define  JTC_COMM_SM_STATE_i_InV_LJB	72
#define  JTC_COMM_SM_STATE_w_InV_LJB	73

#define  JTC_COMM_SM_STATE_s_VERSION	80
#define  JTC_COMM_SM_STATE_w_VERSION	81


#define  JTC_COMM_SM_STATE_ERR			0xFF

////------------------------------------------------------------------------
typedef struct{
	uint8_t type;
	uint8_t ver0;
	uint8_t	ver1;
	uint8_t year0;
	uint8_t year1;
	uint8_t month;
	uint8_t date;
	
	uint8_t app_type;
	uint8_t app_ver0;
	uint8_t app_ver1;
	uint8_t app_year0;
	uint8_t app_year1;
	uint8_t app_month;
	uint8_t app_date;
}JTC_COMM_SOFT_VER, *pJTC_COMM_SOFT_VER;

////------------------------------------------------------------------------
typedef struct{
	
	uint8_t		currState;			///
	uint32_t	monitorTick;		///	use state machine tick
	uint32_t	smTick;				///	add 1 every system tick;
	
	uint32_t 	sm_time;				///
	
	uint8_t		ZJA_DATA[ACK_LEN_CTRL];
	uint8_t		ZJB_DATA[ACK_LEN_CTRL];
	uint8_t		LJB_DATA[ACK_LEN_110V];
	uint8_t		ZJA_errCnt;
	uint8_t		ZJB_errCnt;
	uint8_t		LJB_errCnt;
	
	uint8_t 	JLB_Xfer_cnt;
	
	uint8_t		ex_VersionFlag;
	uint8_t		in_VersionFlag;
	uint8_t		JW_Flag;
	
	JTC_COMM_SOFT_VER SoftVersion[JTC_COMM_VERSION_CNT];
	//---------------------------------
	uint8_t		uartReceiveFlag;	/// set when a new uart frame has been receive
	uint8_t		uartReceiveSrc;
	uint8_t		uartReceiveType;
	uint8_t		uartReceiveCmd;
	uint16_t	uartReceiveDataLen;///
	uint8_t		uartReceiveData[JTC_COMM_SM_RX_DATA_MAX_LEN];
	
	uint8_t		uartTransmitFlag;	/// set when a new uart frame has been receive
	uint8_t		uartTransmitDst;
	uint8_t		uartTransmitType;
	uint8_t		uartTransmitCmd;
	uint16_t	uartTransmitDataLen;///
	uint8_t		uartTransmitData[JTC_COMM_SM_TX_DATA_MAX_LEN];
	
}JTC_COMM_SM, *pJTC_COMM_SM;

extern JTC_COMM_SM uart1_SM;
//------------------------------------------------------------------------------
void JTC_COMM_SM_init(JTC_COMM_SM *sm);
void JTC_COMM_SM_stepTick(JTC_COMM_SM *sm);
void JTC_COMM_SM_main(JTC_COMM_SM *sm);
//------------------
void JTC_COMM_SM_clearRxFlag(JTC_COMM_SM *sm);
void JTC_COMM_SM_setRxFlag(JTC_COMM_SM *sm);
uint8_t JTC_COMM_SM_receiveNewFrame(JTC_COMM_SM *sm);
void JTC_COMM_SM_setRxFrame(JTC_COMM_SM *sm,
								uint8_t src,
								uint8_t type,
								uint8_t cmd,
								uint8_t *dataBuf,
								uint16_t len);
//------------------
uint8_t JTC_COMM_SM_haveFrameToTx(JTC_COMM_SM *sm);
void JTC_COMM_SM_clearTxFlag(JTC_COMM_SM *sm);
void JTC_COMM_SM_setTxFlag(JTC_COMM_SM *sm);
void JTC_COMM_SM_setTxFrame(JTC_COMM_SM *sm,
									uint8_t dst,
									uint8_t type,
									uint8_t cmd,
									uint8_t *dataBuf,
									uint16_t len);
uint8_t JTC_COMM_SM_takeTxFrame(JTC_COMM_SM *sm,
									uint8_t *dst,
									uint8_t *type,
									uint8_t *cmd,
									uint8_t *readDataBuf,
									uint16_t *len);


//------------------------------------------------------------------------------
#endif

///EOF


